package com.bj.disc.dialog

import android.app.Activity
import android.content.DialogInterface
import android.os.Handler
import android.os.Looper
import android.view.View
import android.widget.TextView
import com.ai3d.rfid.R
import com.ai3d.rfid.base.dialog.BaseDialog
import com.ai3d.rfid.common.utils.HandlerUtils
import com.bj.disc.util.StringUtil

/**
 * @ProjectName : Trias
 * @Author : weijinsong
 * @Time : 2023/4/6 9:30
 * @Description : 文件描述
 */
class UpdateDialog(
    var text: String,
    var activity: Activity,
    var callBack: ((isConfirm:Boolean) -> Unit)? = null
) :
    BaseDialog(activity, R.style.AlertDialogThemeNoBackground) {
    var dialogHandler = Handler(Looper.getMainLooper()) {
        changeCancelText()
        number--
        if(number<=0){
            tvCancel?.performClick()
        }else{
            repeat()
        }
        false
    }
    var number=30*60
    private var tvTitle: TextView? = null
    private var tvTips: TextView? = null
    private var tvCancel: TextView? = null
    private var vLine: View? = null
    private var tvConfirm: TextView? = null

    override fun initView() {
        tvTitle = findViewById(R.id.tvTitle)
        tvTips = findViewById(R.id.tvTips)
        tvCancel = findViewById(R.id.tvCancel)
        vLine = findViewById(R.id.vLine)
        tvConfirm = findViewById(R.id.tvConfirm)
        tvTitle?.setText(R.string.hint)
        if(text.isNotEmpty()) {
            tvTips?.text = text
        }

        setOnDismissListener(object :DialogInterface.OnDismissListener{
            override fun onDismiss(p0: DialogInterface?) {
                dialogHandler.removeCallbacksAndMessages(null)
            }

        })
        //dialogHandler.sendEmptyMessageDelayed(1,1*1000)
    }

    fun changeCancelText(){
        tvCancel?.setText(StringUtil.loadString(R.string.no)+"("+formatSeconds()+")")
    }

    fun formatSeconds(): String {
        return if (number < 60) {
            String.format("%ds", number)
        } else {
            val minutes = number / 60
            String.format("%02dmin", number)
        }
    }
    fun repeat(){
        dialogHandler.sendEmptyMessageDelayed(1,1*1000)
    }

    override fun initEvent() {
        tvCancel?.setOnClickListener {
            dismiss()
            callBack?.invoke(false)
        }
        tvConfirm?.setOnClickListener {
            dismiss()
            callBack?.invoke(true)
        }
    }



    override fun initData() {

    }

    override fun getLayoutId(): Int {
        return R.layout.update_dialog
    }

    override fun getAnimId(): Int {
        return -1
    }
}